﻿//participantRole = " Excution||Approve||Varification||Notices";

function onParticipantDrop(e) {
    var $target = $(e.dropTarget);
    if ($target.is('ul.ws-participants')) {
        e.preventDefault();
        var nodetext = getTreeviewSelectedItemText($('#participants'), e);

        var $win = $('#Window').data('tWindow');
        var $newnode = $('.ws-participants').addListItem(null);
        var controltype = $(e.item).attr('controltype');
        initialParticipantLi($newnode, controltype, nodetext);
    }
}

function initialParticipantLi($newnode, controltype, nodetext,id) {
    var $target = $('.ws-participants');
    $newnode.find('.display-text').text(nodetext);

    $newnode.addHoverFunctions({
        deleteEvent: function () {
            $newnode.remove();
        },
        detailEvent: function () {
            setDetailToWindowEditor($newnode, initialParticipantDetail($newnode), $('#Window'));
        }
    });
}

function initialParticipantDetail($target) {
    var data = $target.data('lidata');//数据
    var array = new Array();
    var controltype = $target.attr('controltype');
    var employeenames="";
    if (data && data.Employees) {
        for (var i = 0; i < data.Employees.length; i++) {
            employeenames += data.Employees[i].FullName + ";";
        }
    }

    var options = "";
    $('.ws-conditions').find('li').each(function () {
        if (options == "")
            options = $(this).data('lidata').ConditionName;
        else
            options += "||" + $(this).data('lidata').ConditionName;
    });

    var fieldvaluetext = "";
    if (data && data.Conditions) {
        for (var i = 0; i < data.Conditions.length; i++) {
            if (fieldvaluetext == "")
                fieldvaluetext = data.Conditions[i].ConditionName;
            else
                fieldvaluetext += "||"+ data.Conditions[i].ConditionName;
        }

    }
 
    array.push({ controltype: 'text', fieldname: '角色名称', fieldvalue: data == null ? null : data.Title, must: true });
    array.push({ controltype: 'peoplefinder', fieldname: '角色员工', fieldvalue: employeenames, fieldvaluedata: data == null ? null : data.Employees, must: true });
    array.push({ controltype: 'checkbox', fieldname: '角色条件', fieldvalue: fieldvaluetext, optionalvalue: options,fieldvaluereference: ".ws-conditions li", fieldvaluedata: data == null ? null : data.Conditions, must: false });
    $target.data('av', array);

    var win = $('#Window').data('tWindow');
    var $table = $target.getTable(
                         { save: function () {
                             if ($target.attr('sucess') == 'true') {
                                 $target.find('.display-text').text(array[0].fieldvalue);
                                 $target.attr('uniquename', array[0].fieldvalue);
                                 $target.removeClass('unsave').addClass('saved');
                                 saveParticipantData($target);
                                 win.close();
                             }
                         },
                             cancel: function () {
                                 win.close();
                             }
                         });
    return $table;
}

function saveParticipantData($li) {
    var data = $li.data('av');
        var id = $li.attr('id');
        var workflowid = $('#workflowid').val();
        var lidata = { 
        WorkflowID: workflowid, ID: id,
        Title: data[0].fieldvalue,
        Employees: data[1].fieldvaluedata,
        Conditions: data[2].fieldvalue 
        };

        $li.data('lidata', lidata);
}

function getParticipantsData() {
    var array = new Array();
    $('.ws-participants').children('li').each(function () {
        var $li = $(this);
        var data = $li.data('lidata');
        array.push(data);
    });
    return array;
}

function setParticipantData(data) {
     if(data.length>0) {
       var  $ul = $('.ws-participants');
        for (var i = 0; i < data.length; i++) {
            var $li = $ul.addListItem({ lidata: data[i] });
            var controltype = data[i].ControlType;
            var uniquename = data[i].Title;
            $li.attr('id', data[i].ID);
            $li.attr('uniquename', uniquename)
            initialParticipantLi($li, controltype, uniquename);
        } 
    }
}
